package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.DeviceInfoBean
import com.hinnotek.warehouse.databinding.ActivityDeviceUsegeQueryBinding
import com.hinnotek.warehouse.databinding.ItemDeviceUsageQueryLayoutBinding
import com.hinnotek.warehouse.viewmodel.DeviceUsageViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_DEVICE_USAGE_DETAILS
import com.lee.common.utils.ACTIVITY_DEVICE_USAGE_QUERY
import com.lee.common.utils.Skipping
import com.lee.common.utils.ToastUtils
import dagger.hilt.android.AndroidEntryPoint


/**
 * 设备使用情况查询
 */
@Route(path = ACTIVITY_DEVICE_USAGE_QUERY)
@AndroidEntryPoint
class DeviceUsageQueryActivity : BaseActivity<ActivityDeviceUsegeQueryBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<DeviceInfoBean, ItemDeviceUsageQueryLayoutBinding>
    private val viewModel by viewModels<DeviceUsageViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_device_usege_query)
        setTitle("设备使用情况")

        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_device_usage_query_layout,
            ArrayList(),
            BR.deviceUsageQuery
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
        bindingView.productNumE.addTextChangedListener {
            viewModel.productNum = it.toString()
        }

        adapter.addListener { binding, itemData ->
            binding.root.setOnClickListener {
                Skipping.start(ACTIVITY_DEVICE_USAGE_DETAILS, "DeviceDetail", itemData, this)
            }
        }
        bindingView.search.setOnClickListener {
            val inputData = bindingView.productNumE.text.toString()
            if (inputData == "") {
                ToastUtils.showShort("请输入单号")
                return@setOnClickListener
            }
            viewModel.refresh()
        }
    }

    override fun setData() {
        viewModel.deviceIfoPage.observe(this, Observer {
            adapter.addAll(it, viewModel.isFirst)
        })
    }
}